package com.yz.lzq.liwushuo.ui.modules.home.boutique.gridview.christmas;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.decoration.SpacesItemDecoration;
import com.yz.lzq.liwushuo.jsonbean.home.boutique.christmas.ChristmasBean;
import com.yz.lzq.liwushuo.ui.modules.comminterface.OnMyItemClickListener;
import com.yz.lzq.liwushuo.ui.webview.home.HomeWebContentActivity;
import com.yz.lzq.liwushuo.utils.ActivityUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ChristmasActivity extends AppCompatActivity implements ChristmasContract.ChristmasViewInterface, View.OnClickListener {

    @Bind(R.id.imageView_christmas_back)
    ImageView imageViewChristmasBack;
    @Bind(R.id.textView_christmas_tittle)
    TextView textViewChristmasTittle;
    @Bind(R.id.imageView_christmas_share)
    ImageView imageViewChristmasShare;
    @Bind(R.id.recyclerview_christmas_activity)
    XRecyclerView recyclerviewChristmas;
    private Context mContext = this;
    private boolean isRefreshing;
    private boolean isLoadingMore;
    private ChristmasPrisenter prisenter = null;
    private String target_url = null;
    private List<ChristmasBean.DataBean.PostsBean> mList = new ArrayList<>();
    private ChristmasrAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_christmas);
        ButterKnife.bind(this);
        prisenter = new ChristmasPrisenter(this);
        target_url = getIntent().getStringExtra("targetUrl");
        String tittle = getIntent().getStringExtra("tittle");
        textViewChristmasTittle.setText(tittle);

        initRecyclerView();
        prisenter.showRecycleView(false);
        imageViewChristmasBack.setOnClickListener(this);
        imageViewChristmasShare.setOnClickListener(this);
    }

    private void initRecyclerView() {
        final LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerviewChristmas.setLayoutManager(manager);
        recyclerviewChristmas.addItemDecoration(new SpacesItemDecoration(5));
        recyclerviewChristmas.setHasFixedSize(true);
        //下拉刷新 和 上拉加载
        recyclerviewChristmas.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                isRefreshing = !isRefreshing;
                prisenter.showRecycleView(false);
            }

            @Override
            public void onLoadMore() {
                isLoadingMore = !isLoadingMore;
                prisenter.showRecycleView(true);

            }
        });
        adapter = new ChristmasrAdapter(mContext, mList, new OnMyItemClickListener() {
            @Override
            public void clickMyItem(View view, int position) {
                ActivityUtil.statActivity(mContext, HomeWebContentActivity.class, mList.get(position).getId());
            }
        });
        recyclerviewChristmas.setAdapter(adapter);
    }

    @Override
    public String getUrlID() {
//        Log.i("tag", "getUrlID================>>>>>>>>>>>: " + target_url.substring(target_url.lastIndexOf("=") + 1));
        return target_url.substring(target_url.lastIndexOf("=") + 1);
    }

    @Override
    public void getRecyclerViewBean(ChristmasBean bean) {
//        Log.i("tag", "getRecyclerViewBean========>>>>>>>>: " + bean);
        if (mList.size() == 0) {
            mList.addAll(bean.getData().getPosts());
            adapter.reloadRecyclerView(bean.getData().getPosts(), true);
//            Log.i("tag", "getRecyclerViewBean:============================== 这走了啊。。。。。。。。");
        } else if (isRefreshing) {
            mList.clear();
            mList.addAll(bean.getData().getPosts());
            adapter.reloadRecyclerView(mList, true);
            recyclerviewChristmas.refreshComplete();
            isRefreshing = !isRefreshing;
        } else if (isLoadingMore) {
            mList.addAll(bean.getData().getPosts());
            adapter.reloadRecyclerView(mList, true);
            recyclerviewChristmas.loadMoreComplete();
            isLoadingMore = !isLoadingMore;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageView_christmas_back:
                finish();
                break;
            case R.id.imageView_christmas_share:
                break;
        }
    }
}
